package com.example.myapplication

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.databinding.DataBindingUtil
import com.example.myapplication.R
import com.example.myapplication.databinding.ActivityMainBinding
import com.example.myapplication.face.FaceActivity
import com.example.myapplication.flutter.AndroidToFlutter
import com.example.myapplication.flutter.MainActivity2
import com.example.myapplication.rxjava.RxjavaErrorActivity
import com.orhanobut.logger.Logger
import io.dcloud.feature.barcode2.BarcodeProxy
import io.flutter.embedding.android.FlutterActivity
import io.dcloud.feature.barcode2.BarcodeProxy.context
import io.dcloud.feature.sdk.DCUniMPJSCallback

import io.dcloud.feature.sdk.DCUniMPSDK
import io.dcloud.feature.sdk.DCUniMPSDK.IOnUniMPEventCallBack
import org.json.JSONObject
import java.lang.Exception


class MainActivity : AppCompatActivity() {
    var binding: ActivityMainBinding? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
        binding!!.button12.setOnClickListener { click: View? ->
            Intent(this, RxjavaErrorActivity::class.java).apply {
                startActivity(this)
            }
        }
        binding!!.button7.setOnClickListener { click: View? ->
            Intent(this, MainActivity2::class.java).apply {
                startActivity(this)
            }
        }
        binding!!.button15.setOnClickListener { click: View? ->
            Intent(this, FaceActivity::class.java).apply {
                startActivity(this)
            }
        }

        binding!!.button8.setOnClickListener { click: View? ->
            try {

                val arguments = JSONObject()
                arguments.put("name", "tangchao")
                arguments.put("id", "kotlin")
                DCUniMPSDK.getInstance().startApp(
                    this, "__UNI__FDF3485", "pages/demo20211126/demo1126?name=tangchao&id=111"
                )
//                DCUniMPSDK.getInstance().startApp(
//                    this,
//                    "__UNI__FDF3485",
//                    "pages/demo20211126/demo1126?" + arguments.toString()
//                )

            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
        DCUniMPSDK.getInstance().setDefMenuButtonClickCallBack(object :
            DCUniMPSDK.IMenuButtonClickCallBack {
            override fun onClick(appid: String?, id: String?) {
                when (id) {
                    "gy" -> Toast.makeText(this@MainActivity, "在小程序里面点击了", Toast.LENGTH_SHORT)
                        .show();
                    else -> Logger.d("MainActivity===onClick: 59");

                }
            }
        })

        DCUniMPSDK.getInstance().setUniMPOnCloseCallBack(object : DCUniMPSDK.IUniMPOnCloseCallBack {
            override fun onClose(p0: String?) {
                Logger.d("Mai设置小程序被关闭事件监听==" + p0);

            }
        })
        //        设置监听小程序发送给宿主的事件
        DCUniMPSDK.getInstance().setOnUniMPEventCallBack { event, data, callback ->
            Logger.d("onUniMPEventReceive    event=$event")
            //回传数据给小程序
            callback.invoke("设置监听小程序发送给宿主的事件")
        }

        var s1 = DCUniMPSDK.getInstance().getAppBasePath(this@MainActivity)
        Logger.d("MainActivity===onCreate: 获取uni小程序运行路径" + s1);
        var vesion = DCUniMPSDK.getInstance().getAppVersionInfo("__UNI__FDF3485")
        vesion?.let {
            Logger.d("获取已运行过的小程序应用版本信息,没有运行过的小程序是无法正常获取到版本信息的。返回值需要判空处理!!!" + vesion);

        }
    }
}